<template>
	<view class="register">
		<Navbar>注册</Navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="content">
				<view class="title">
					<view>注册账号</view>
				</view>
				<view class="item">
					<view class="key">请输入账号</view>
					<input type="text" placeholder="请输入手机号" class="input" v-model="phone">
					<view class="input_code">
						<input type="text" placeholder="请输入验证码" autocomplete="off" v-model="code">
						<view class="get_code" v-if="isTimer">{{time}}s</view>
						<view class="get_code" v-else @click="getCode">获取验证码</view>
					</view>
				</view>
				<view class="item">
					<view class="key">密码</view>
					<input type="text" password placeholder="请输入密码" class="input" v-model="password">
				</view>
				<view class="item">
					<view class="key">确认密码</view>
					<input type="text" password placeholder="请再次输入密码" class="input" v-model="confirm_password">
				</view>
				<view class="item">
					<view class="key">支付密码</view>
					<input type="text" password placeholder="请输入密码" class="input" v-model="pay_password">
				</view>
				<view class="item">
					<view class="key">确认支付密码</view>
					<input type="text" password placeholder="请再次输入密码" class="input" v-model="payconfirm_password">
				</view>
				<view class="item">
					<view class="key">邀请码</view>
					<input type="text" placeholder="请输入邀请码" class="input" v-model="invitation_code">
				</view>
				<view class="explain" @click="checked=!checked">
					<image :src="`${baseImageUrl}/common/checked.png`" v-if="checked"></image>
					<image :src="`${baseImageUrl}/common/check_no.png`" v-else></image>
					<view class="tips">
						<text>阅读并同意</text>
						<text class="desc" @click.stop="router('/pages/setting/article?key=agreement')">《用户协议》</text>
						<text>和</text>
						<text class="desc" @click.stop="router('/pages/setting/article?key=privacy')">《隐私协议》</text>
					</view>
				</view>
				<view class="btn" @click="submit">注册</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		smsSend,
		register
	} from '@/request/api.js';
	import {
		captchaAppId
	} from "@/utils/config";
	export default {
		data() {
			return {
				checked: false,
				phone: "",
				code: "",
				password: "",
				confirm_password: "",
				pay_password: "",
				payconfirm_password: "",
				invitation_code: ""
			}
		},
		onLoad(option) {
			this.invitation_code = option.code;
		},
		onUnload() {
			clearInterval(this.timer);
		},
		methods: {
			async getCode() {
				if (!this.phone) {
					this.$u.toast("请输入手机号");
					return false;
				};
				const captcha = new TencentCaptcha(captchaAppId, async (res) => {
					if (res.ret !== 0) return false;
					let {
						randstr,
						ticket
					} = res;
					this.isTimer = true;
					this.timer = setInterval(() => {
						this.time--
						if (this.time < 0) {
							this.isTimer = false;
							this.time = 60;
							clearInterval(this.timer);
						};
					}, 1000);
					await smsSend({
						phone: this.phone,
						randstr: randstr,
						ticket: ticket,
						scene: "register"
					});
					this.$u.toast("验证码已发送,请查看");
				}, {});
				captcha.show();
			},
			async submit() {
				if (!this.checked) {
					this.$u.toast("请先阅读《协议》");
					return false;
				}
				let {
					token,
					data
				} = await register({
					phone: this.phone,
					code: this.code,
					password: this.password,
					confirm_password: this.confirm_password,
					pay_password: this.pay_password,
					payconfirm_password: this.payconfirm_password,
					invitation_code: this.invitation_code
				});
				let role_id = data.role_id;
				this.$u.toast("注册成功");
				this.$store.commit('changeToken', token);
				setTimeout(() => {
					if (role_id == 0) {
						this.router("/pages/login/role");
					} else {
						this.redirectRouter(3);
					};
				}, 1000);
			}
		}
	}
</script>

<style scoped lang="scss">
	.register {
		height: 100vh;
		background: #29170D;
		overflow: hidden;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight});
			padding: 30rpx;
			box-sizing: border-box;

			.content {
				width: 688rpx;
				height: 1321rpx;
				padding: 40rpx 55rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/688_1321.png) no-repeat;
				background-size: 100% 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;

				.title {
					display: flex;
					justify-content: center;
					align-items: center;

					view {
						width: 256rpx;
						height: 30rpx;
						line-height: 30rpx;
						text-align: center;
						color: #494338;
						font-size: 42rpx;
						font-weight: 400;
						font-family: LiSu;
						background: url(#{$baseImageUrl}/home/title.png) no-repeat;
						background-size: 100% 100%;
					}
				}

				.item {
					width: 100%;

					.key {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin-bottom: 20rpx;
					}

					.input {
						width: 100%;
						height: 80rpx;
						color: #494338;
						font-size: 28rpx;
						font-weight: 400;
						font-family: SimSun;
						border-radius: 10rpx;
						padding-left: 45rpx;
						box-sizing: border-box;
						box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
						background: #C0B5B6;
					}

					.input_code {
						height: 80rpx;
						margin-top: 40rpx;
						border-radius: 10rpx;
						padding-left: 45rpx;
						padding-right: 36rpx;
						box-sizing: border-box;
						box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
						background: #C0B5B6;
						display: flex;
						align-items: center;

						input {
							flex: 1;
							height: 80rpx;
							color: #494338;
							font-size: 28rpx;
							font-family: SimSun;
						}

						.get_code {
							color: #FC2B1D;
							font-size: 28rpx;
							font-weight: 400;
							font-family: SimSun;
						}
					}
				}

				.explain {
					width: 100%;
					display: flex;
					justify-content: flex-start;
					align-items: center;

					image {
						width: 52rpx;
						height: 52rpx;
						margin-right: 10rpx;
					}

					.tips {
						color: #494338;
						font-size: 24rpx;
						font-weight: 400;
						font-family: SimSun;

						.desc {
							color: #21b3e9;
						}
					}
				}

				.btn {
					width: 204rpx;
					height: 74rpx;
					line-height: 74rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: 30rpx;
					font-weight: 400;
					font-family: SimSun;
					background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
					background-size: 100% 100%;
				}
			}
		}
	}
</style>